$(document).ready(function(){
	var $basicInfoForm = $('#u-i-form');
		$pic = $('.pic'),
		$preImg = $('.pre-img'),
		$uploadBtn = $('.upload-btn');
		isUpload = false;
		uploadImgUrl = $('.old-pic-url').val();
		tipsPic = $('.modal .tips-pic')[0],
		$tips = $('.modal .tips-content'),
		$dialogBox = $('#dialog-box');
		$modalFooter = $('.modal-footer');
		//表单字段
		$uname = $('#uname');
		$sex = $('.sex');
		$tel =$('#tel');
		$email =$('#email');
		//提交
		$basicInfoSubmit = $('#basic-info-submit');
	$pic.change(function(){
		var file = this.files ;
		if(file[0].size>1.5*1024*1024){
			$tips.html('图片太大啦');
			tipsPic.className="tips-pic pic-warn";
			$dialogBox.modal({
				backdrop:false,
				keyboard:false
			});
		}else{
			var xhr = new XMLHttpRequest();
			 var fd = new FormData();
	       //关联表单数据,可以是自定义参数
	       fd.append("name", file.name);
	       fd.append("fileName", this.files[0]);
	       xhr.addEventListener("load", uploadComplete, false);
	       $pic.attr('disabled','disabled');
	       $uploadBtn.html('<span class="loading-pic"></span>');
	       //发送文件和表单自定义参数
	       xhr.open("POST", "/bookcrossing/upload");
	       xhr.send(fd);
		}
	});
	//基本信息表单提交
	$basicInfoForm.submit(function(e){
		e.preventDefault();
		var tipStr = '',
			$where = $('');
		if($.trim($uname.val())==''){
			tipStr = '名字不能空哦';
			$where = $uname;
		}else if($.trim($tel.val())==''){
			tipStr = '手机不能空哦';
			$where = $tel;
		}else if($.trim($email.val())==''){
			tipStr = '邮箱不能空哦';
			if(!BC.checkEmail($email.val())){
				tipStr = "邮箱格式不对哦";
			}
			$where = $email;
		}
		if(tipStr!=''){
			$tips.html(tipStr);
			tipsPic.className="tips-pic pic-warn";
			$dialogBox.modal({
				backdrop:false,
				keyboard:false
			});
			$where.focus();
		}else{
			BC.ajax({
				type:'POST',
				url:'/bookcrossing/user/infoEdit',
				data:$basicInfoForm.serialize()+'&picurl='+uploadImgUrl+'&isUpload='+isUpload,
				b:function(){
					$basicInfoSubmit.html('<span class="loading-pic"></span>').attr('disabled','disabled');
				},
				s:function(resp){
					$tips.html(resp.msg);
					tipsPic.className="tips-pic pic-success";
					$modalFooter.remove();
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
					window.setTimeout(function(){
						window.location.reload();
					},1000);
				},
				f:function(resp){
					$tips.html(resp.msg);
					tipsPic.className="tips-pic pic-warn";
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
				},
				e:function(){
					$tips.html('哎呀，网络不好呀，换个姿势再试一试');
					tipsPic.className="tips-pic pic-warn";
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
				},
				c:function(){
					$basicInfoSubmit.html('填好啦').removeAttr('disabled');
				}
			});
		}
	});

	//修改密码部分
	var $changePswForm = $('#change-psw-form'),
		$oldpsw = $('#oldpsw'),
		$psw = $('#psw'),
		$confirmpsw = $('#confirmpsw'),
		$changePswBtn = $('#change-psw-btn');
	$changePswForm.submit(function(e){
		e.preventDefault();
		var tipStr = '';
		if($.trim($oldpsw.val())==''){
			tipStr = '旧密码不能空哦';
		}else if($.trim($psw.val())==''){
			tipStr = '新密码不能空哦';
		}else if($.trim($confirmpsw.val())==''){
			tipStr = '确认密码不能空哦';
		}else if(!BC.checkPassword($.trim($psw.val())) || !BC.checkPassword($.trim($confirmpsw.val()))){
			tipStr = '密码长度不不对哦，6-20个字符';
		}else if($.trim($psw.val())!=$.trim($confirmpsw.val())){
			tipStr = '两次密码输入不正确哦';
		}
		if(tipStr!=''){
			$tips.html(tipStr);
			tipsPic.className="tips-pic pic-warn";
			$dialogBox.modal({
				backdrop:false,
				keyboard:false
			});
		}else{
			BC.ajax({
				type:'POST',
				url:'/bookcrossing/user/changePassword',
				data:{oldpsw:hex_md5($.trim($oldpsw.val())),psw:hex_md5($.trim($psw.val()))},
				b:function(){
					$changePswBtn.html('<span class="loading-pic"></span>').attr('disabled','disabled');
				},
				s:function(resp){
					$tips.html(resp.msg+'，请重新登录');
					tipsPic.className="tips-pic pic-success";
					$('.ok-btn').click(function(){
						window.location.href='/bookcrossing/logout';
					});
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
				},
				f:function(resp){
					$tips.html(resp.msg);
					tipsPic.className="tips-pic pic-warn";
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
				},
				e:function(){
					$tips.html('哎呀，网络不好呀，换个姿势再试一试');
					tipsPic.className="tips-pic pic-warn";
					$dialogBox.modal({
						backdrop:false,
						keyboard:false
					});
				},
				c:function(){
					$changePswBtn.html('填好啦').removeAttr('disabled');
				}
			});
		}
	});
	
	//图片上传后的事件
	function uploadComplete(ev){
		if(ev.target.responseText){
			var data = JSON.parse(ev.target.responseText);
			if(data.success==true){
				BC.createPreImg($pic[0],$preImg[0]);
				isUpload = true ;
				uploadImgUrl = data.url;
			}else{
				$tips.html('上传失败了');
				tipsPic.className="tips-pic pic-warn";
				$dialogBox.modal({
					backdrop:false,
					keyboard:false
				});
			}
			$pic.removeAttr('disabled');
	    	$uploadBtn.html('<span class="glyphicon glyphicon-upload"></span> 重新上传');
		}
	}
});